﻿#pragma once

#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>

#include "land_def.h"

#ifdef LAND_WINDOWS
    #ifndef WIN32_LEAN_AND_MEAN
       #define WIN32_LEAN_AND_MEAN
    #endif

    #ifdef LAND_BASE_EXPORTS
        #ifdef LAND_BASE_CORE
            #define LAND_BASE_API    __declspec(dllexport)
        #else
            #define LAND_BASE_API    __declspec(dllimport)
        #endif
    #else
        #define LAND_BASE_API		extern
    #endif
#else
    #ifdef LAND_BASE_EXPORTS
        #define LAND_BASE_API 		extern
     #else
        #define LAND_BASE_API
    #endif
#endif

#ifdef LAND_WINDOWS
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#endif


#ifdef LAND_WINDOWS
#define LAND_FD                 SOCKET
#define LAND_FD_ERROR           SOCKET_ERROR
#define LAND_INVALID_FD         INVALID_SOCKET
#else
#define LAND_FD                 int
#define LAND_FD_ERROR           -1
#define LAND_INVALID_FD         -1
#endif

#undef LAND_SOCKET_USE_EPOLL
#undef LAND_SOCKET_USE_KQUEUE
#ifdef LAND_SOCKET_USE_SELECT
#else
    #ifdef LAND_WINDOWS
        #define LAND_SOCKET_USE_SELECT
    #endif

    #ifdef LAND_LINUX
        #define LAND_SOCKET_USE_EPOLL
    #endif

    #ifdef LAND_APPLE
        #ifdef LAND_SYS_MACOS
            #define LAND_SOCKET_USE_KQUEUE
        #endif
        #ifdef LAND_SYS_IOS
            #define LAND_SOCKET_USE_SELECT
        #endif
    #endif

    #ifdef LAND_ANDROID
        #define LAND_SOCKET_USE_SELECT
    #endif

#endif // #ifndef LAND_SOCKET_USE_SELECT


#ifdef LAND_SOCKET_USE_SELECT
#ifndef LAND_SYS_WIN
#include <sys/select.h>
#endif

#define LAND_SOCKETPOLL_READ   (1)
#define LAND_SOCKETPOLL_WRITE  (1 << 1)
#define LAND_SOCKETPOLL_ERROR  (1 << 2)
#define LAND_SOCKETPOLL_BE_ERROR (1 << 3)
#endif


#ifdef LAND_SOCKET_USE_EPOLL
#include <sys/epoll.h>

#define LAND_SOCKETPOLL_READ   EPOLLIN
#define LAND_SOCKETPOLL_WRITE  EPOLLOUT
#define LAND_SOCKETPOLL_ERROR  (EPOLLERR | EPOLLHUP)
#endif


#ifdef LAND_SOCKET_USE_KQUEUE
#include <sys/event.h>

#define LAND_SOCKETPOLL_READ   1 //EVFILT_READ
#define LAND_SOCKETPOLL_WRITE  2 //EVFILT_WRITE
#define LAND_SOCKETPOLL_ERROR  0
#endif


#define LAND_SOCKPOLL_SETSIZE  64
typedef uint64_t LandHandle;
typedef uint64_t LandSession;


typedef float LandFloat;
#define landCos(X) cos(X)
#define landSin(X) sin(X)